package work.jame.blog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.jame.blog.framework.constant.ArticleOrderRuleConstant;
import work.jame.blog.framework.constant.SearchTypeConstant;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.dto.SearchDTO;
import work.jame.blog.pojo.vo.HomeDataVO;
import work.jame.blog.pojo.vo.SearchVO;
import work.jame.blog.service.ArticleService;
import work.jame.blog.service.BulletinService;
import work.jame.blog.service.UserService;

/**
 * @author : Jame
 * @date : 2023-06-09 11:19
 **/
@Service
public class HomeService {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private BulletinService bulletinService;

    @Autowired
    private UserService userService;


    /**
     * 查询首页数据
     *
     * @return vo
     */
    public HomeDataVO queryHomeData() {
        HomeDataVO vo = new HomeDataVO();
        vo.setArticleSynopsis(articleService.queryHomeArticleSynopsis(1L, 10L,
                ArticleOrderRuleConstant.PUBLISH_TIME));
        vo.setBulletins(bulletinService.queryLimit10());
        return vo;
    }

    public SearchVO search(SearchDTO searchDTO) {
        String keyword = searchDTO.getKeyword();
        Long pageNumber = searchDTO.getPageNumber();
        Long pageSize = searchDTO.getPageSize();
        SearchVO vo = new SearchVO();
        if (SearchTypeConstant.ARTICLE_TITLE == searchDTO.getType()) {
            vo.setArticleSynopsis(articleService.queryHomeArticleSynopsis(keyword, pageNumber, pageSize,
                    ArticleOrderRuleConstant.PUBLISH_TIME));
        } else if (SearchTypeConstant.USER_NICK_NAME == searchDTO.getType()) {
            vo.setUsers(userService.searchUserByNickName(keyword, pageNumber, pageSize));
        } else if (SearchTypeConstant.BULLETIN_TITLE == searchDTO.getType()) {
            vo.setBulletins(bulletinService.searchBulletinByKeyword(keyword, pageNumber, pageSize));
        } else {
            throw new BusinessException("搜索类型不存在");
        }
        return vo;
    }
}
